//
//  CarCell.m
//  FuelLog
//
//  Created by Pure Mass Communications on 1/11/09.
//  Copyright 2009 . All rights reserved.
//
//  Placed under GPL v2 with the kind permission of Pure Mass Communications
//
//  Modified to make compile under SDK 3.1 by Olaf Greck in May 2010
//
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#import "CarCell.h"
#import "FuelLogAppDelegate.h"
#import "FuelLogViewController.h"
#import "AddCarViewController.h"

#import "DebugOutput.h"
@implementation CarCell

@synthesize car, master;
@synthesize titleLabel;

/*
- (void)reallyDelete {
	if (self.car.pk != -1) {
		FuelLogAppDelegate *appDelegate = (FuelLogAppDelegate *)[[UIApplication sharedApplication] delegate];
		[appDelegate removeCar:self.car];
		UITableView *tv = (UITableView *)self.superview;
		[tv reloadData];
		[master clickSelectedCar];
	}
}

#pragma mark UIAlertView

- (void)alertDeleteCancelAction
{
	// open a alert with an OK and cancel button
	NSString *msg = [[NSString alloc] initWithFormat:@"%@ %@ %@", @"Remove", self.car.label, @"and all of its fuel entries?"];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:msg
				delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes, Delete", nil];
	[alert show];
	[alert release];
	[msg release];
}

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if (buttonIndex == [alert cancelButtonIndex]) {
		return;
	}
	[self reallyDelete];
}
*/
 
# pragma mark UITableViewCell

- (id)initWithFrame:(CGRect)frame // reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithFrame:frame]) // reuseIdentifier:reuseIdentifier])
	{
#pragma mark ex SDK 2.0, no substitution
				self.target = self;
		self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
#pragma mark ex SDK 2.0, substitted with accessoryButtonTappedForRowWithIndexPath in CarsViewController
/*
		float version = [[[UIDevice currentDevice] systemVersion] floatValue];
		debug(@"have detected %f OS",version);
		// does detect running OS version, but is not needed here
		if (version < 3.0)
		{*/
			// iPhone 2.2.1 code here
		self.accessoryAction = @selector(onClick:);
/*			debug(@"have detected 2.2.1 OS");
		}
*/		
		// cell's title label
		titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
		titleLabel.backgroundColor = [UIColor clearColor];
		titleLabel.opaque = NO;
		titleLabel.textColor = [UIColor blackColor];
		titleLabel.highlightedTextColor = [UIColor whiteColor];
		titleLabel.font = [UIFont boldSystemFontOfSize:18.0];
		[self.contentView addSubview:titleLabel];
		
		// cell's check button
		checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
		checkButton.frame = CGRectZero;
		checkButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		checkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
		[checkButton addTarget:self action:@selector(check:) forControlEvents:UIControlEventTouchDown];
		checkButton.backgroundColor = [UIColor clearColor];
		[self.contentView addSubview:checkButton];

/*
		// cell's remove button
		UIImage *image = [UIImage imageNamed:@"remove-19.png"];
		removeButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
		removeButton.frame = CGRectZero;
		removeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		removeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
		[removeButton addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchDown];
		removeButton.backgroundColor = [UIColor clearColor];
		[removeButton setBackgroundImage:image forState:UIControlStateNormal];
		[self.contentView addSubview:removeButton];

		// cell's export button
		exportButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		exportButton.frame = CGRectZero;
		exportButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		exportButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
		[exportButton addTarget:self action:@selector(exportAction:) forControlEvents:UIControlEventTouchDown];
		exportButton.backgroundColor = [UIColor clearColor];
		[exportButton setTitle:@"export" forState:UIControlStateNormal];
		[self.contentView addSubview:exportButton];
 */
	}
	return self;
}

// called when the accessory view (disclosure button) is touched OS 2.2.1 only
- (void)onClick:(id)sender
{
	debug(@"in onCLick");
	AddCarViewController *addController = [FuelLogViewController addCarViewController];
    Car *theCar = self.car;
	[theCar retain];
	[addController setCar:theCar];
	[addController setLabel];
	[master presentModalViewController:addController animated:YES];
}

- (void)layoutSubviews
{
	[super layoutSubviews];
//	debug(@"in layoutSubviews");
    CGRect contentRect = [self.contentView bounds];
	
	CGRect frame = CGRectMake(contentRect.origin.x + 40.0, 8.0, contentRect.size.width, 30.0);
	titleLabel.frame = frame;
	titleLabel.text = self.car.label;
	
	// layout the check button image
	UIImage *checkedImage = [UIImage imageNamed:@"checked.png"];
	frame = CGRectMake(contentRect.origin.x + 10.0, 12.0, checkedImage.size.width, checkedImage.size.height);
	checkButton.frame = frame;
	
	UIImage *image = self.car.selected ? checkedImage: [UIImage imageNamed:@"unchecked.png"];
	UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
	[checkButton setBackgroundImage:newImage forState:UIControlStateNormal];

/*	
	frame = CGRectMake(contentRect.origin.x + 40.0, 12.0, 20.0, 20.0);
	removeButton.frame = frame;

	frame = CGRectMake(contentRect.origin.x + 200.0, 12.0, 56.0, 20.0);
	exportButton.frame = frame;
*/ 
}

- (void)dealloc
{
	[titleLabel release];
	[checkButton release];
	[car release];
    [super dealloc];
}

// called when the checkmark button is touched
- (void)check:(id)sender 
{
	FuelLogAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
	[appDelegate chooseCar:self.car];
	[master clickSelectedCar];
}

- (void)uncheck {
	UIImage *checkImage = [UIImage imageNamed:@"unchecked.png"];
	[checkButton setImage:checkImage forState:UIControlStateNormal];	
}

- (void)checkAction:(id)sender
{
	// note: we don't use 'sender' because this action method is called separate from the button (i.e. from table selection)
	FuelLogAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
	[appDelegate chooseCar:self.car];
	UIImage *checkImage = (self.car.selected) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];
	[checkButton setImage:checkImage forState:UIControlStateNormal];
}

/*
- (void)removeAction:(id)sender {
	[self alertDeleteCancelAction];
}

- (void)exportAction:(id)sender {
}
*/
 
@end
